<?php
/**
 * 矿机
 */
namespace app\common\model;

use app\common\common;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;
use think\Model;
use app\common\model\MemberMining as MemberMiningModel;
use app\common\model\Member as MemberModel;
use app\common\model\MiningSpecification as MiningSpecificationModel;

class Mining extends Model
{
    protected $name = 'mining';

    /**
     * 列表
     * @param $order
     * @param int $limit
     * @param int $page
     */
    public static function getList($order,$limit = 20,$page = 1,$where=[]){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $data;
    }

    /**
     * 查询某个矿机
     * @param $id    查询条件
     * @return Mining|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDetails($id,$field=''){
         $info = self::where('id',$id);
         if(!empty($field)) $info = $info->field($field);
        return $info->find();
    }


    /**
     * 执行释放矿机
     */
    public static function mining_release(){

        //查询所有已开启矿机
        $mining_list = self::where('status',1)->select()->toArray();

        //释放矿机
        Db::startTrans();
        try {
            //循环矿机
            foreach ($mining_list as $v){
                $total = 0;//总产出

                //查询所有矿机套餐
                $MiningSpecification_list = MiningSpecificationModel::getListAll(['pid'=>$v['id']]);


                if(!empty($MiningSpecification_list)){  //套餐不为空

                    foreach ($MiningSpecification_list as $ms){
                        //查询当前套餐所有用户订单
                        $member_mining = MemberMiningModel::getListAll(['msid'=>$ms['id'],'status'=>1]);
                        if(!empty($member_mining)){
                            //循环用户矿机订单
                            foreach ($member_mining as $mm){    //释放

                                //数量 * 释放数量
                                $daily_release = bcmul($ms['daily_release'],$mm['num'],2);

                                //发放矿机奖励
                                UserMoneyLogModel::change($mm['mid'],'lcc',$daily_release,'Mining machine output',$mm['id'],'矿机产出');

                                //矿机见点奖
                                common::maxtrix_mlevel($mm['mid'],$daily_release);

                                //统计已发放总数
                                $total = bcadd($total,$daily_release,2);
                                //当前订单产出+1天
                                $day = $mm['day'] +1;
                                $up = [
                                    'day'   =>  $day,  //已产出天数
                                    'output'    => bcadd($mm['output'],$daily_release,2), //已产出
                                    'status'    =>  $day == $mm['endtime'] ? 2 : 1, //产出完成就结束
                                    'update_time'   =>  date('Y-m-d H:i:s',time()),
                                ];
                                MemberMiningModel::upproduce($mm['id'],$up);
                            }
                        }
                    }
                }
                if($total > 0){
                    //计算总释放
                    $release_num = bcadd($v['release_num'],$total,4);
                    //计算销毁
                    $destruction_num = bcadd($v['destruction_num'],bcmul($total,$v['destruction_ratio']/100,4),4);
                    //记录
                    TaskLog::add('每日矿机释放',1,date('Y-m-d',time()).'释放:'.$total.',销毁:'.$destruction_num,'每日矿机释放成功');
                    //统计 总释放 + 虚拟释放 以及销毁 = 总量
                    $total_num = bcadd(bcadd(bcadd($release_num,$destruction_num,4),$v['virtual_destruction'],4),$v['virtual_release'],4);
                    $save = [
                        'release_num'=>$release_num,    //实际总释放
                        'destruction_num'=>$destruction_num,    //实际总销毁
                        'status'    =>  $total_num >= $v['total_num'] ? 2 : 1,  //矿机状态
                    ];
                    self::where('id',$v['id'])->save($save);
                    if($save['status'] == 2){   //停止所有套餐出售  停止所有矿机订单
                        //停止套餐出售
                        MiningSpecificationModel::where('pid',$v['id'])->save(['status'=>3]);
                        //停止订单
                        MemberMiningModel::where('miid',$v['id'])->save(['status'=>2]);
                    }
                }

            }

            Db::commit();
            return true;
        } catch (\Exception $exception) {
            Db::rollback();
            TaskLog::add('每日矿机释放',2,json_encode($mining_list),'释放失败');
            return false;
        }
    }

}